package menuActionDao

import (
	"gitee.com/yangguoliang001/go-gin-vue3/BO"
	"gitee.com/yangguoliang001/go-gin-vue3/model"
)

func (m *MenuActionDao) QueryMenuActionListByMenuId(menuID int32) (menuList []model.MenuActionDo, err error) {
	err = m.DB.Debug().Table("menu_action").Where("menu_id=?", menuID).Find(&menuList).Error
	return
}

func (m *MenuActionDao) QueryMenuActionAllList(menuIds []int32) (menuList []model.MenuActionDo, err error) {
	for _, v := range menuIds {
		var menuListItem []model.MenuActionDo
		menuListItem, err = m.QueryMenuActionListByMenuId(v)
		menuList = append(menuList, menuListItem...)
	}
	return
}

func (m *MenuActionDao) QueryMethodList() (methodList []BO.MethodBO, err error) {
	err = m.DB.Debug().Table("menu_action").Select("distinct method").Find(&methodList).Error
	return
}

func (m *MenuActionDao) AddMenuAction(menuAction model.MenuActionDo) (int64, error) {
	//
	result := m.DB.Debug().Table("menu_action").FirstOrCreate(&menuAction, "menu_id = ? AND method = ? AND api = ?", menuAction.MenuId, menuAction.Method, menuAction.Api)
	return result.RowsAffected, result.Error
}

func (m *MenuActionDao) DeleteMenuAction(id string) (err error) {
	//err = m.DB.Debug().Table("menu_action").Where("id = ?", id).Update("is_deleted", 1).Error
	// 改为直接删除
	err = m.DB.Debug().Table("menu_action").Where("id = ?", id).Delete(&model.MenuActionDo{}).Error
	return
}
